package com.atguigu.gulimail.ware.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimail.ware.feign.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.ware.dao.WmsWareSkuDao;
import com.atguigu.gulimail.ware.entity.WmsWareSkuEntity;
import com.atguigu.gulimail.ware.service.WmsWareSkuService;
import org.springframework.transaction.annotation.Transactional;


@Service("wmsWareSkuService")
public class WmsWareSkuServiceImpl extends ServiceImpl<WmsWareSkuDao, WmsWareSkuEntity> implements WmsWareSkuService {

    @Autowired
    ProductService productService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        final QueryWrapper<WmsWareSkuEntity> wrapper = new QueryWrapper<>();
        final IPage<WmsWareSkuEntity> page1 = new Query<WmsWareSkuEntity>().getPage(params);

        String skuIdStr = (String) params.get("skuId");
        if (!StringUtils.isBlank(skuIdStr)) {
            long skuId = Long.parseLong(skuIdStr);
            if (skuId > 0) {
                wrapper.eq("sku_id", skuId);
            }
        }
        final String wareIdStr = (String) params.get("wareId");
        if (!StringUtils.isBlank(wareIdStr)) {
            long wareId = Long.parseLong(wareIdStr);
            if (wareId > 0) {
                wrapper.eq("ware_id", wareId);
            }
        }

        IPage<WmsWareSkuEntity> page = this.page(page1, wrapper);
        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void addStock(WmsWareSkuEntity wmsWareSku) {

        final WmsWareSkuEntity wmsWareSkuEntity =
                this.baseMapper.selectOne(new QueryWrapper<WmsWareSkuEntity>()
                        .eq("sku_id", wmsWareSku.getSkuId())
                        .eq("ware_id", wmsWareSku.getWareId())
                );
        if (null == wmsWareSkuEntity) {
            try {
                final R info = productService.getSkuInfo(wmsWareSku.getSkuId());
                if (0 == (Integer) info.get("code")) {
                    Map<String, Object> skuInfo = (Map<String, Object>) info.get("skuInfo");
                    wmsWareSku.setSkuName((String) skuInfo.get("skuName"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.baseMapper.insert(wmsWareSku);
        } else {
            this.baseMapper.addStock(wmsWareSku.getSkuId(), wmsWareSku.getWareId(),
                    wmsWareSku.getStock());
        }
    }

    @Override
    public Long getStockBySkuId(Long skuId) {

        final QueryWrapper<WmsWareSkuEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("sku_id", skuId);
        final List<WmsWareSkuEntity> wmsWareSkuEntities = baseMapper.selectList(wrapper);
        Long stockCount = 0L;
        for (WmsWareSkuEntity wmsWareSkuEntity : wmsWareSkuEntities) {

            int lastCount = wmsWareSkuEntity.getStock() - wmsWareSkuEntity.getStockLocked();
            if (lastCount > 0) {
                stockCount += lastCount;
            }
        }
        return stockCount;
    }
}